package com.dp.appi.interceptor;

import com.dp.appi.service.ProjectService;
import com.dp.appi.util.Constant;
import com.dp.appi.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;

/**
 * Created by bchen on 2015/3/25.
 * 拦截器
 */
public class APPInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        //验证用户是否登陆
        String openIdent = request.getParameter("openIdent");
        String openIdentuser = request.getParameter("openIdentuser");

        if (openIdentuser == null || "".equals(openIdentuser))
        {
            StringBuffer buf = new StringBuffer();
            buf.append("{\"code\":\"" + SYS.L10003.name() + "\",\"msg\":\"" + SYS.L10003.getValue() + "\"}");
            response.setHeader("Content-type", "text/plain;charset=UTF-8");
            response.getOutputStream().write(buf.toString().getBytes());
            response.getOutputStream().flush();
            return false;
        }
       else
        {
            if(Constant.loginmap.containsValue(openIdentuser))
            {
                if (openIdent == null || "".equals(openIdent))
                {
                    StringBuffer buf = new StringBuffer();
                     buf.append("{\"code\":\"" + SYS.L10003.name() + "\",\"msg\":\"" + SYS.L10003.getValue() + "\"}");

                    response.setHeader("Content-type", "text/plain;charset=UTF-8");
                    response.getOutputStream().write(buf.toString().getBytes());
                    response.getOutputStream().flush();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                StringBuffer buf = new StringBuffer();
                buf.append("{\"code\":\"" + SYS.L10003.name() + "\",\"msg\":\"" + SYS.L10003.getValue() + "\"}");
                response.setHeader("Content-type", "text/plain;charset=UTF-8");
                response.getOutputStream().write(buf.toString().getBytes());
                response.getOutputStream().flush();
                return false;
            }
        }


    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {

    }

}
